package com.ktgj.hotel.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.hotel.domain.rooms.HotelGraphicRequest;
import com.ktgj.hotel.domain.rooms.HotelGraphicResponse;
import com.ktgj.hotel.domain.rooms.HotelHomeDetailInfo;
import com.ktgj.hotel.domain.rooms.HotelHomeFacilityInfo;
import com.ktgj.hotel.service.IHotelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.hotel.domain.common.HotelInfo;
import com.ktgj.hotel.service.IHotelInfoService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 酒店详情Controller
 * 
 * @author ruoyi
 * @date 2021-11-22
 */
@ControllerAdvice
@RestController
@RequestMapping("/hotelInfo")
public class HotelInfoController extends BaseController
{
    @Autowired
    private IHotelInfoService hotelInfoService;

    @Autowired
    private IHotelService hotelService;

    /**
     * 查询酒店详情列表
     */
    @PreAuthorize(hasPermi = "hotel:info:list")
    @GetMapping("/list")
    public TableDataInfo list(HotelInfo hotelInfo)
    {
        startPage();
        List<HotelInfo> list = hotelInfoService.selectHotelInfoList(hotelInfo);
        return getDataTable(list);
    }

    /**
     * 导出酒店详情列表
     */
    @PreAuthorize(hasPermi = "hotel:info:export")
    @Log(title = "酒店详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HotelInfo hotelInfo) throws IOException
    {
        List<HotelInfo> list = hotelInfoService.selectHotelInfoList(hotelInfo);
        ExcelUtil<HotelInfo> util = new ExcelUtil<HotelInfo>(HotelInfo.class);
        util.exportExcel(response, list, "酒店详情数据");
    }

    /**
     * 获取酒店详情详细信息
     */
    @PreAuthorize(hasPermi = "hotel:info:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(hotelInfoService.selectHotelInfoById(id));
    }

    /**
     * 新增酒店详情
     */
    @PreAuthorize(hasPermi = "hotel:info:add")
    @Log(title = "酒店详情", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HotelInfo hotelInfo)
    {
        return toAjax(hotelInfoService.insertHotelInfo(hotelInfo));
    }

    /**
     * 修改酒店详情
     */
    @PreAuthorize(hasPermi = "hotel:info:edit")
    @Log(title = "酒店详情", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HotelInfo hotelInfo)
    {
        return toAjax(hotelInfoService.updateHotelInfo(hotelInfo));
    }

    /**
     * 删除酒店详情
     */
    @PreAuthorize(hasPermi = "hotel:info:remove")
    @Log(title = "酒店详情", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(hotelInfoService.deleteHotelInfoByIds(ids));
    }

    /**
     * 酒店介绍
     * @param map
     * @return
     */
    @PostMapping("/queryHotelDetailInfo")
    public AjaxResult queryHotelDetailInfo(@RequestBody Map<String, Object> map) {
        String hotelId = map.get("hotelId").toString();
        HotelHomeDetailInfo hotelHomeDetailInfo;
        try {
            hotelHomeDetailInfo = hotelService.queryHotelDetail(hotelId);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        if (hotelHomeDetailInfo != null) {
            return AjaxResult.success(hotelHomeDetailInfo);
        } else {
            return AjaxResult.error("信息查询失败,请稍后再试");
        }
    }

    /**
     * 设施服务
     * @param map
     * @return
     */
    @PostMapping("/queryHotelFacilityInfo")
    public AjaxResult queryHotelFacilityInfo(@RequestBody Map<String, Object> map) {
        String hotelId = map.get("hotelId").toString();
        HotelHomeFacilityInfo hotelHomeFacilityInfo;
        try {
            hotelHomeFacilityInfo = hotelInfoService.queryHotelFacilityInfo(hotelId);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        if (hotelHomeFacilityInfo != null) {
            return AjaxResult.success(hotelHomeFacilityInfo);
        } else {
            return AjaxResult.error("信息查询失败,请稍后再试");
        }
    }

    /**
     * 酒店房型页图片
     * @param request
     * @return
     */
    @PostMapping("/hotelGraphicIntroduction")
    public AjaxResult hotelGraphicIntroduction(@RequestBody HotelGraphicRequest request) {
        HotelGraphicResponse response;
        try {
            response = hotelInfoService.hotelGraphicIntroduction(request);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        if (response.getProductUrls().size() > 0) {
            return AjaxResult.success(response);
        } else {
            return AjaxResult.error("图片查询失败,请稍后再试!");
        }
    }
}
